//
//  NSObject+HUD.m
//  BaseProject
//
//  Created by liulei on 16/5/17.
//  Copyright © 2016年 liulei. All rights reserved.
//

#import "NSObject+HUD.h"
#import <MBProgressHUD.h>

@implementation NSObject (HUD)
//获取当前屏幕的最上方正在显示的那个view
- (UIView *)currentView{
    return (UIView*)[UIApplication sharedApplication].delegate.window;
}
/** 弹出文字提示 菊花模式*/
- (void)showChrysanthemumAlert:(NSString *)text{
    
    //防止在非主线程中调用此方法,会报错
    dispatch_async(dispatch_get_main_queue(), ^{
        //    弹出新的提示之前,先把旧的隐藏掉
        //        [self hideProgress];
        [MBProgressHUD hideAllHUDsForView:[self currentView] animated:YES];
        MBProgressHUD *progressHUD=[MBProgressHUD showHUDAddedTo:[self currentView] animated:YES];
        progressHUD.mode = MBProgressHUDModeText;
        progressHUD.bezelView.backgroundColor = HEXACOLOR(000000, 0.3);
        progressHUD.detailsLabelText = text;
        progressHUD.detailsLabelFont = [UIFont systemFontOfSize:16.0];
        progressHUD.mode = MBProgressHUDModeIndeterminate;
        // 隐藏时候从父控件中移除
        progressHUD.removeFromSuperViewOnHide = YES;
        [progressHUD hide:YES afterDelay:7];
    });
    
}

/** 弹出文字提示 */
- (void)showAlert:(NSString *)text{
    //防止在非主线程中调用此方法,会报错
    dispatch_async(dispatch_get_main_queue(), ^{
        //    弹出新的提示之前,先把旧的隐藏掉
        //        [self hideProgress];
        [MBProgressHUD hideAllHUDsForView:[self currentView] animated:YES];
        MBProgressHUD *progressHUD=[MBProgressHUD showHUDAddedTo:[self currentView] animated:YES];
        progressHUD.mode = MBProgressHUDModeText;
        progressHUD.bezelView.backgroundColor = HEXACOLOR(000000, 0.3);
        progressHUD.detailsLabelText = text;
        progressHUD.detailsLabelFont = [UIFont systemFontOfSize:16.0];
//        progressHUD.mode = MBProgressHUDModeIndeterminate;
        // 隐藏时候从父控件中移除
        progressHUD.removeFromSuperViewOnHide = YES;
        [progressHUD hide:YES afterDelay:1.5];
    });
}
/** 显示忙 */
- (void)showBusy{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        
        //        [self hideProgress];
        [MBProgressHUD hideAllHUDsForView:[self currentView] animated:YES];
        MBProgressHUD *progressHUD=[MBProgressHUD showHUDAddedTo:[self currentView] animated:YES];
        // 隐藏时候从父控件中移除
        progressHUD.removeFromSuperViewOnHide = YES;
        //最长显示5秒
        [progressHUD hide:YES afterDelay:8];
        
    }];
    
}
- (void)showMsg:(NSString *)msg duration:(CGFloat)time imgName:(NSString *)imgName{
    
    //    弹出新的提示之前,先把旧的隐藏掉
    //        [self hideProgress];
    [MBProgressHUD hideAllHUDsForView:[self currentView] animated:YES];
    
    MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:[self currentView] animated:YES];
    
    // 显示模式,改成customView,即显示自定义图片(mode设置,必须写在customView赋值之前)
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = HEXACOLOR(000000, 0.6);
    // 设置要显示 的自定义的图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
    // 显示的文字,比如:加载失败...加载中...
    hud.label.text = msg;
    hud.label.textColor = kWhiteColor;
    // 标志:必须为YES,才可以隐藏,  隐藏的时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hideAnimated:YES afterDelay:time];
}
/** 隐藏提示 */
- (void)hideProgress{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [MBProgressHUD hideAllHUDsForView:[self currentView] animated:YES];
    }];
}
@end








